//
// SplitterChannel.cpp
//
// $Id: //poco/1.4/Foundation/src/SplitterChannel.cpp#1 $
//
// Library: Foundation
// Package: Logging
// Module:  SplitterChannel
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier:	BSL-1.0
//


#include "Poco/SplitterChannel.h"
#include "Poco/StringTokenizer.h"


namespace Poco {


SplitterChannel::SplitterChannel()
{
}


SplitterChannel::~SplitterChannel()
{
	try
	{
		close();
	}
	catch (...)
	{
		poco_unexpected();
	}
}


void SplitterChannel::addChannel(Channel_Type type,Channel* pChannel)
{
	poco_check_ptr (pChannel);

	FastMutex::ScopedLock lock(m_mutex);
	auto it = m_channels.find(type);
	if (it != m_channels.end())
	{
		it->second->release();
	}
	pChannel->duplicate();
	m_channels[type] = pChannel;
}


void SplitterChannel::removeChannel(Channel_Type type)
{
	FastMutex::ScopedLock lock(m_mutex);
	auto it = m_channels.find(type);
	if (it != m_channels.end())
	{
		it->second->release();
		m_channels.erase(it);
	}
}


Channel* SplitterChannel::getChannel(Channel_Type type)
{
	FastMutex::ScopedLock lock(m_mutex);
	auto it = m_channels.find(type);
	if (it != m_channels.end())
		return it->second;
	return NULL;
}

void SplitterChannel::log(const string& msg)
{
    FastMutex::ScopedLock lock(m_mutex);

	for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
	{
		it->second->log(msg);
	}
}

void SplitterChannel::log(const string& msg,LogPriority prio)
{
   	FastMutex::ScopedLock lock(m_mutex);

	for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
	{
		it->second->log(msg,prio);
	}
}
        
void SplitterChannel::log(const string& msg,LogPriority prio,const char* file,int line)
{
	FastMutex::ScopedLock lock(m_mutex);

	for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
	{
		it->second->log(msg,prio,file,line);
	}
}

void SplitterChannel::close()
{
	FastMutex::ScopedLock lock(m_mutex);

	for (auto it = m_channels.begin(); it != m_channels.end(); ++it)
	{
		it->second->release();
	}
	m_channels.clear();
}


int SplitterChannel::count() const
{
	FastMutex::ScopedLock lock(m_mutex);
	
	return (int) m_channels.size();
}


} // namespace Poco
